namespace Yaaf.WP7.NativeAccess.ComWrapper.LG.COM_WP7STIDriver
{
    using System;

    using Microsoft.Phone.InteropServices;

    using Yaaf.Wp7.AdvancedExplorer;
    using Yaaf.WP7.NativeAccess.Helper;

    internal class MfgTest : IMTDAsyncResultCallback, IDisposable
    {
        private IMfgTest native;

        public MfgTest()
        {
            if (ComBridge.RegisterComDll("COM_WP7STIDriver.dll", new Guid("36E202C1-BBEE-46BA-BBB7-899936D3DCC1")) != 0)
            {
                throw new RegisterComException("RegisterComDll Failed.");
            }
            native = (IMfgTest)new CMfgTest();

            native.RegisterForAsyncResultUpdate(this);
        }

        public void ProvisionXml(string xml)
        {
            var returnCode = native.ChangeConfigXML(xml);
            if (returnCode != 0)
            {
                Logger.Instance.WriteLine("LG Provisioning (ReturnCode: {0}, XMLDATA: {1})", returnCode, xml);
            }
        }

        public void AsyncResultUpdate(string result)
        {
            Logger.Instance.WriteLine("LG AsyncResultUpdate (Data: {0})", result);
        }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~MfgTest()
        {
            this.Dispose(false);
        }

        private void Dispose(bool disposing)
        {

            if (native != null)
            {
                native.UnregisterAsyncResultUpdate();
            }
        }
    }
}